small American Flag Ms Access Gurus

VBA > Report > Draw American Flag

Draw an American flag on an Access report, any size, any position, using VBA.

Quick Jump

video tutorials

explain how the code works

YouTube: Draw American Flag with Access using VBA (8:34)


background image

Access drew the flag that was used as background image in the Word document that data from Access is merged into.

YouTube: Merge Data from Access into a Microsoft Word Letter (17:10)

Screen Shot

You can draw a big flag on a page

American Flag

or small ones, and fade the colors

4 American Flags diagonally down page with faded colors

Goto Top  

Code

Module

'*************** Code Start *****************************************************
' reference:
'  http://msaccessgurus.com/VBA/Code/AmericanFlag.htm
' module: mod_FlagAmerican_s4p
'-------------------------------------------------------------------------------
' Purpose  : Draw an American flag on an Access report, any size, any position.
' Author   : crystal (strive4peace)
' License  : below code
' Code List: www.msaccessgurus.com/code.htm
'-------------------------------------------------------------------------------
'           FlagAmerican
'-------------------------------------------------------------------------------

Public Sub FlagAmerican( _ 
    pReport As Report _ 
   ,ByVal pX As Single,ByVal pY As Single _ 
   ,ByVal pMaxWidth As Single _ 
   ,ByVal pMaxHeight As Single _ 
   ,Optional ByVal piColorSet As Integer = 0 _ 
   ) 
'200611 strive4peace, 200629
'called from code behind report on page to draw flag -- or send report object
'draw flag at pX, pY
'proportions for the American Flag are hard-coded based on the height
'measurements from:
'  https://www.ushistory.org/betsy/flagetiq3.html
'
'measurements in TWIPS
'uses font for stars: Wingdings 2

   Dim x2Flag As Single,y2Flag As Single _ 
   ,dXFlag As Single,dYFlag As Single _ 
   ,dXUnion As Single,dYUnion As Single _ 
   ,dYStripe As Single _ 
   ,dXStar As Single,dYStar As Single _ 
   ,HeightStar As Single,WidthStar As Single _ 
   ,sgFlagWidth2Height As Single _ 
   ,x1 As Single,y1 As Single _ 
   ,x2 As Single,y2 As Single _ 
   ,sgStarFontSize As Single _ 
   ,nBlue As Long _ 
   ,nRed As Long _ 
   ,nWhite As Long _ 
   ,nBlack As Long _ 
   ,i As Integer _ 
   ,j As Integer _ 
   ,iDrawWidth As Integer _ 
   ,sStar As String 
   
   If piColorSet <> 0 Then 
      'faded colors
      nBlue = 16772590  'RGB(238,237,255)
      nRed = 15127295  'RGB(255,210,230)
      nBlack = 14474460  'RGB(220,220,220)
   Else 
      'regular colors
      nBlue = 7223353  'RGB(57, 56, 110)
      nRed = 3218867  'RGB(179, 29, 49)
      nBlack = 0 
   End If 
   
   nWhite = 16777215  'RGB(255, 255, 255)
   
   'proportion for width:height
   sgFlagWidth2Height = 1.9 

   iDrawWidth = 2 
      
   'make sure proportions are right
   If pMaxHeight > pMaxWidth / sgFlagWidth2Height Then 
      dXFlag = pMaxWidth 
      dYFlag = pMaxWidth / sgFlagWidth2Height 
   Else 
      dXFlag = pMaxWidth * sgFlagWidth2Height 
      dYFlag = pMaxHeight 
   End If 
   
   'x2 and y2 for flag
   x2Flag = pX + dXFlag 
   y2Flag = pY + dYFlag 
   
      'size of union
   dXUnion = 0.76 * dYFlag 
   dYUnion = 7 / 13 * dYFlag 
   
   'height of stripe = 1/13 flag height
   dYStripe = dYFlag / 13 
   
   '--------------------- stripes
   x1 = pX + dXUnion 
   x2 = x2Flag 
   
   'draw red stripes, skip white stripes
   'stripes next to union
   For i = 1 To 7 Step 2 
      y1 = pY + (i - 1) * dYStripe 
      y2 = y1 + dYStripe 
      
      pReport.Line (x1,y1)-(x2,y2),nRed,BF 
   Next i 
   
   'stripes below union
   x1 = pX 
   For i = 9 To 13 Step 2 
      y1 = pY + (i - 1) * dYStripe 
      y2 = y1 + dYStripe 
      
      pReport.Line (x1,y1)-(x2,y2),nRed,BF 
   Next i 
   
   '--------------------- union
   
   'draw filled rectangle for union
   pReport.Line (pX,pY)-(pX + dXUnion,pY + dYUnion),nBlue,BF 
   
   '--------------------- stars
   'character to use for star
   sStar = Chr(234)  'font="Wingdings 2"
   
   dXStar = 0.063 * dYFlag 
   dYStar = 0.054 * dYFlag 
   sgStarFontSize = 0.0616 * dYFlag / 20 
   
   With pReport 
      .DrawWidth = iDrawWidth   'line width is 2 pixels
      .FontSize = sgStarFontSize  'iFontSize
      .FontName =  "Wingdings 2"
      .ForeColor = nWhite 
      
      WidthStar = .TextWidth(sStar) 
      HeightStar = .TextHeight(sStar) 
      
      y1 = pY + dYStar - HeightStar / 2 
	        
      For j = 1 To 5 
         x1 = pX + dXStar - WidthStar / 2 
         .CurrentY = y1      
		 
         For i = 1 To 6 
            .CurrentX = x1  
            .Print sStar 
            x1 = x1 + (2# * dXStar) 
         Next i 
         
         x1 = pX + (2 * dXStar) - WidthStar / 2 
         y1 = y1 + dYStar 
         .CurrentY = y1 		 
         
         If j <> 5 Then 
            For i = 1 To 5 
               .CurrentX = x1 
               .Print sStar 
               x1 = x1 + (2# * dXStar) 
            Next i 
         End If 
         
         y1 = y1 + dYStar 
         
      Next j 
      
   End With 
   
   'draw rectangle for flag
   pReport.Line (pX,pY)-(x2Flag,y2Flag),nBlack,B 
      
End Sub 

Goto Top  

Code behind reports

'-------------------------------------------------------------------------------
' Purpose  : draw a full page American flag
' Author   : crystal (strive4peace)
' License  : below code
' Code List: www.msaccessgurus.com/code.htm
'-------------------------------------------------------------------------------
'  NEEDS module:
'    mod_FlagAmerican_s4p
'-------------------------------------------------------------------------------

Private Sub Report_Page() 
'200612 strive4peace
'draw a full page American flag
   ' CALLS
   '  FlagAmerican
   
   Dim X As Single,Y As Single _ 
      ,dX As Single,dY As Single 
      
   With Me 
      .ScaleMode = 1  'twips
      X = .ScaleLeft 
      Y = .ScaleTop 
      dX = .ScaleWidth 
      dY = .ScaleHeight 
   End With 
   
   'draw a full page flag-- limited by proportional dimensions of flag
   Call FlagAmerican(Me,X,Y,dX,dY) 
   
End Sub 
'-------------------------------------------------------------------------------
' Purpose  : draw 4 flags diagonally down the page, faded color
' Author   : crystal (strive4peace)
' License  : below code
' Code List: www.msaccessgurus.com/code.htm
'-------------------------------------------------------------------------------
'  NEEDS module:
'    mod_FlagAmerican_s4p
'-------------------------------------------------------------------------------

Private Sub Report_Page() 
'200612 strive4peace
'draw 4 flags diagonally down the page, faded color
   ' CALLS
   '  FlagAmerican
   
   Dim X As Single,Y As Single _ 
      ,dX As Single,dY As Single _ 
      ,i As Integer 
      
   With Me 
      .ScaleMode = 1  'twips
      X = .ScaleLeft 
      Y = .ScaleTop 
      dX = .ScaleWidth / 4 
      dY = .ScaleHeight / 4 
   End With 
   
   For i = 1 To 4 
      'draw a quarter page flag-- limited by proportional dimensions of flag
      'color set <> 0 for faded colors
      Call FlagAmerican(Me,X,Y,dX,dY,1) 
      X = X + dX 
      Y = Y + dY 
   Next i 
   
End Sub 

'   You may freely use and share this code
'     provided this license notice and comment lines are not changed;
'     code may be modified provided you clearly note your changes.
'   You may not sell this code alone, or as part of a collection,
'     without my handwritten permission.
'   All ownership rights reserved. Use at your own risk.
'   ~ crystal (strive4peace)  www.msaccessgurus.com
'*************** Code End *******************************************************`

Keyword and comments in code were colored with Color Code add-in

Goto Top  

Logic

The basic structure of the code to draw flags is similar to code for the CalendarMaker.

It needs a report object, XY coordinates of upper left, maximum width and height (unlike the CalendarMaker, which stretches to what is sent, the flag code uses dimensions from American flag measurements), and, optionally faded colors instead of normal.

Measurements are in TWIPs (TWenty In a Point). There are 1440 twips in an inch.

The font used for the star is Wingdings 2, Chr(234). The report.print method doesn't render ChrW characters properly, so I had to find a star in a font.

Goto Top  

Reference

Report object

Docs / Office VBA Reference / Access / Object model / Report object

Help: Report object

Line

Docs / Office VBA Reference / Access / Object model / Report object / Methods / Line

Help: Report.Line method

Print

Docs / Office VBA Reference / Access / Object model / Report object / Methods / Print

Help: Report.Print method

TextHeight

Docs / Office VBA Reference / Access / Object model / Report object / Methods / TextHeight

Help: Report.TextHeight method

TextWidth

Docs / Office VBA Reference / Access / Object model / Report object / Methods / TextWidth

Help: Report.TextWidth method

CurrentX

Docs / Office VBA Reference / Access / Object model / Report object / Properties / CurrentX

Help: Report.CurrentX property

CurrentY

Docs / Office VBA Reference / Access / Object model / Report object / Properties / CurrentY

Help: Report.CurrentY property

FontName

Docs / Office VBA Reference / Access / Object model / Report object / Properties / FontName

Help: Report.FontName property

FontSize

Docs / Office VBA Reference / Access / Object model / Report object / Properties / FontSize

Help: Report.FontSize property

ForeColor

Docs / Office VBA Reference / Access / Object model / Report object / Properties / ForeColor

Help: Report.ForeColor property

Chr function

Docs / Office VBA Reference / Language reference / Reference / Functions / Conversion functions / Chr

Help: Chr function

Goto Top  

Backstory

I love celebrating holidays. Independence Day was always a day for watching parades, eating ice cream, and waving flags. In these uncertain times, I don't know how much celebration will happen, but I honor those who stood up and fought for what's right and laid the rules in place to ensure our rights and freedom to pursue life, liberty, and happiness.

Goto Top  

Share with others

here's the link for this page in case you want to copy it:
http://msaccessgurus.com/VBA/Code/AmericanFlag.htm

Share your comments

Email me anytime at info@msAccessGurus.com. I love hearing about what you're doing with Access.

Are you looking for help with your application?

Let's connect and do it together. As needed, I'll pull in code and features from my vast libraries, cutting out lots of development time.

Or maybe you have graphics you want to be able to use on reports ... an image or logo that Access could draw? or maybe indicators like stoplights on records? That would be fun to figure out!

I'm happy to help you! I like working with people who want to do it themself, and just need someone to guide past the obstacles and teach better ways. For training and programming, email me at training@msAccessGurus.com

I look forward to hearing from you ~

~ crystal

Goto Top